<template>
  <view class="dialog-mask" v-if="visible" @tap="handleMaskClick">
    <view class="dialog-container" @tap.stop>
      <!-- 标题 -->
      <view class="dialog-header">
        <text class="dialog-title">修改电话</text>
      </view>
      
      <!-- 输入框 -->
      <view class="dialog-body">
        <input 
          class="phone-input" 
          type="number" 
          placeholder="请输入手机号码" 
          v-model="phoneNumber"
          maxlength="11"
          @input="handleInput"
        />
      </view>
      
      <!-- 底部按钮 -->
      <view class="dialog-footer">
        <button class="btn-cancel" @tap="handleCancel">取消</button>
        <button class="btn-confirm" @tap="handleConfirm" :disabled="!isValidPhone">确认</button>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "PhoneEditDialog",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      phoneNumber: this.value
    }
  },
  computed: {
    isValidPhone() {
      return /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(this.phoneNumber)
    }
  },
  watch: {
    value(newVal) {
      this.phoneNumber = newVal
    }
  },
  methods: {
    handleInput(e) {
      this.phoneNumber = e.detail.value
    },
    
    handleCancel() {
      this.$emit('update:visible', false)
      this.$emit('cancel')
    },
    
    handleConfirm() {
      if (this.isValidPhone) {
        this.$emit('update:visible', false)
        this.$emit('confirm', this.phoneNumber)
        this.$emit('input', this.phoneNumber)
		this.phoneNumber = ''
      }
    },
    
    handleMaskClick() {
      this.handleCancel()
    }
  }
}
</script>

<style scoped>
.dialog-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.dialog-container {
  width: 280px;
  background-color: #ffffff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.dialog-header {
  padding: 20px 16px 16px;
  border-bottom: 1px solid #f0f0f0;
  text-align: center;
}

.dialog-title {
  font-size: 16px;
  font-weight: 600;
  color: #333333;
}

.dialog-body {
  padding: 20px 16px;
}

.phone-input {
  width: 100%;
  height: 44px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  color: #333333;
  background-color: #fafafa;
}

.phone-input:focus {
  border-color: #007aff;
  background-color: #ffffff;
}

.dialog-footer {
  display: flex;
  border-top: 1px solid #f0f0f0;
}

.btn-cancel, .btn-confirm {
  flex: 1;
  height: 44px;
  border: none;
  background: none;
  font-size: 14px;
  font-weight: 500;
}

.btn-cancel {
  color: #666666;
  border-right: 1px solid #f0f0f0;
}

.btn-cancel:active {
  background-color: #f5f5f5;
}

.btn-confirm {
  color: #007aff;
}

.btn-confirm:disabled {
  color: #cccccc;
}

.btn-confirm:active:not(:disabled) {
  background-color: #f5f5f5;
}
</style>